package com.work.yshop.mvp.presenter;

import com.work.yshop.model.ChatMessage;
import com.work.yshop.model.Message;
import com.work.yshop.mvp.contract.ChatContract;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 聊天页面的Presenter实现类
 */
public class ChatPresenter extends ChatContract.Presenter {
    private Message originalMessage;
    private int contactId;
    private String latestContent;
    
    @Override
    public void initChat(Message message) {
        if (isViewAttached()) {
            this.originalMessage = message;
            this.contactId = message.getId();
            view.initChatInfo(message);
            loadChatHistory(contactId);
        }
    }
    
    @Override
    public void loadChatHistory(int contactId) {
        if (!isViewAttached()) return;
        
        view.showLoading();
        
        // 模拟从数据库或网络加载聊天记录
        List<ChatMessage> chatMessages = mockChatHistory(contactId);
        
        if (chatMessages.isEmpty()) {
            view.hideLoading();
            view.showChatMessages(new ArrayList<>());
            return;
        }
        
        view.hideLoading();
        view.showChatMessages(chatMessages);
        view.scrollToLatestMessage();
    }
    
    @Override
    public void sendMessage(String content) {
        if (!isViewAttached() || content == null || content.trim().isEmpty()) return;
        
        // 创建一条新消息
        ChatMessage message = new ChatMessage(contactId, content, ChatMessage.TYPE_SENT);
        
        // 保存最新发送的内容，用于更新原始消息
        this.latestContent = content;
        
        // 在UI上添加消息
        view.addNewMessage(message);
        view.scrollToLatestMessage();
        view.clearInputBox();
        
        // 模拟发送消息的网络请求
        // 在实际应用中，这里应该调用数据层的方法发送消息
        mockSendMessage(message);
    }
    
    @Override
    public void updateOriginalMessage(Message originalMessage, String latestContent) {
        if (originalMessage != null && latestContent != null && !latestContent.isEmpty()) {
            originalMessage.setContent(latestContent);
            originalMessage.setUnread(false);
            
            // 更新时间
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.getDefault());
            String currentTime = sdf.format(new Date());
            originalMessage.setTime(currentTime);
        }
    }
    
    /**
     * 获取最新发送的消息内容
     */
    public String getLatestContent() {
        return latestContent;
    }
    
    /**
     * 模拟聊天历史数据
     */
    private List<ChatMessage> mockChatHistory(int contactId) {
        List<ChatMessage> chatMessages = new ArrayList<>();
        
        // 获取当前时间的毫秒数
        long currentTime = System.currentTimeMillis();
        
        // 模拟几条聊天记录
        ChatMessage message1 = new ChatMessage(contactId, "您好，有什么可以帮您的吗？", ChatMessage.TYPE_RECEIVED);
        message1.setTimestamp(currentTime - 3600000); // 1小时前
        chatMessages.add(message1);
        
        ChatMessage message2 = new ChatMessage(contactId, "我想了解一下你们的产品", ChatMessage.TYPE_SENT);
        message2.setTimestamp(currentTime - 3540000); // 59分钟前
        chatMessages.add(message2);
        
        ChatMessage message3 = new ChatMessage(contactId, "好的，我们有各种新鲜水果，现在正值草莓季节，我们的草莓特别甜", ChatMessage.TYPE_RECEIVED);
        message3.setTimestamp(currentTime - 3480000); // 58分钟前
        chatMessages.add(message3);
        
        // 如果有原始消息的内容，添加到聊天记录中
        if (originalMessage != null && originalMessage.getContent() != null) {
            ChatMessage lastMessage = new ChatMessage(contactId, originalMessage.getContent(), ChatMessage.TYPE_SENT);
            lastMessage.setTimestamp(currentTime - 60000); // 1分钟前
            chatMessages.add(lastMessage);
        }
        
        return chatMessages;
    }
    
    /**
     * 模拟发送消息
     */
    private void mockSendMessage(ChatMessage message) {
        // 模拟网络延迟
        new android.os.Handler().postDelayed(() -> {
            if (isViewAttached()) {
                // 模拟接收到回复
                ChatMessage reply = new ChatMessage(contactId, "收到您的消息：" + message.getContent(), ChatMessage.TYPE_RECEIVED);
                view.addNewMessage(reply);
                view.scrollToLatestMessage();
            }
        }, 1000);
    }
} 